﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KCMS.BusinessLogic.Interfaces;
using KCMS.DataAcsess.Interfaces;
using KCMS.BusinessLogic.Models;
using AutoMapper;
using StructureMap;
namespace KCMS.BusinessLogic.Engine
{
    public class PageService:IPageService
    {
        private IPageManager pgm = ObjectFactory.GetInstance<IPageManager>();
        public Models.Page getById(Guid id)
        {
            DataAcsess.Entitys.PageEntity pageEntity = pgm.getById(id);
            return Mapper.Map<DataAcsess.Entitys.PageEntity, Models.Page>(pageEntity);
        }
        public Models.Page getByCodeIndex(String code,int index)
        {

           
            var pageEntity = (from t in pgm.getAll()
                              where (t.Code.Equals(code) && t.Index.Equals(index))
                              select t).SingleOrDefault();
            return Mapper.Map<DataAcsess.Entitys.PageEntity, Models.Page>(pageEntity);
        }

        public IEnumerable<Models.Page> getAll()
        {
            return Mapper.Map<IEnumerable<DataAcsess.Entitys.PageEntity>, IEnumerable<Models.Page>>(pgm.getAll());
            
        }

        public void update(Page page)
        {
            page.DateOfChange = DateTime.Now;
            pgm.update(Mapper.Map<Page,DataAcsess.Entitys.PageEntity>(page));
        }


        public void save(Page page)
        {
            page.DateOfCreate = DateTime.Now;
            page.DateOfChange = DateTime.Now;
            pgm.save(Mapper.Map< Page,DataAcsess.Entitys.PageEntity>(page));
        }


        public void delete(Guid id)
        {
           pgm.delete(id);
        }



        public Page getByCode(string code)
        {
            var pageEntity = (from t in pgm.getAll()
                              where (t.Code.Equals(code))
                              select t).SingleOrDefault();
            return Mapper.Map<DataAcsess.Entitys.PageEntity, Models.Page>(pageEntity);
        }
    }
}
